/**
 * Created by Lwang on 2017/3/6.
 */

/**
 * Created by Lwang on 2017/3/5.
 * 所有文章
 *
 */

import { square,ajax} from './resize.js';
import {way} from "./common.js";
import $ from "jquery";
import {weChat} from "./weChat.js";
var Handlebars = require("./handlebars.js");
square(document,window);
var firstequal = location.href.indexOf("="),
    firstand = location.href.indexOf("&");
const courseid = firstand >-1 ? location.href.substring(firstequal+1,firstand) : location.href.substr(firstequal+1);



$(function () {
    var ispause = true;
    let teacherHeadImg = "",
        teachimg = "";
    Handlebars.registerHelper("isShow",function (v1,v2,options) {
        if(v1 == v2){
            return "display:block";
        }else{
            return "display:none";
        }
    });

    Handlebars.registerHelper("textTohtml",function (str) {
        return new Handlebars.SafeString(str);
    });

    way._login(() => {
        $.ajax({
            url: ajax.teacherInfo,
            xhrFields: {withCredentials: true},
            success: (res) => {
                teachimg = res.headImg;

            }
        });
    });
    //初始化信息
    initContent();
    function initContent() {
        $.ajax({
            url: ajax.userCourse(courseid),
            type:"GET",
            xhrFields: {withCredentials: true},
            success:(res) => {
                var contents = res.contents,
                    d = new Date(res.createTime),
                    firstImg = "",
                    firstWord = "",
                    wordArr = [],
                    picArr = [];
                //处理文章详情列表
                var myTemplate = Handlebars.compile($("#listTemplate").html());
                $("#courseDetail").html(myTemplate(contents));


                document.title = res.title;
                $(".ae-img").find("img").attr("src",res.teacherHeadImg);
                teacherHeadImg = res.teacherHeadImg;
                $(".aet-title").html(res.title);
                $(".aet-date").html((d.getMonth() + 1) +"-"+ d.getDate()+" "+d.getHours()+":"+ d.getSeconds());
                $(".aet-num").html(res.count);

                //图片大小处理
                $.each($("#courseDetail li").find(".aec-picture").find("img"), function (index, obj) {
                    way.picSizeCtrl(obj, ".aec-picture");
                });
                //初始化录音
                $(".aec-audio").click(function () {
                    toPlay(this);
                });

                //分享配置
                $.each(contents,(index,obj) => {
                    if(obj.contentType == "IMAGE"){
                        picArr.push(obj.content);
                    }else if(obj.contentType == "TEXT"){
                        wordArr.push(obj.content);
                    }
                });

                picArr.length > 0 ? firstImg = picArr[0] : "http://m.studypointshare.com/images/c-eg.png";
                wordArr.length > 0 ? firstWord = wordArr[0] : "http://m.studypointshare.com/user-articles.html?courseid="+ res.id;

                //分享初始化
                weChat.shareExe({
                    title: res.title, // 分享标题
                    desc: firstWord, // 分享描述
                    link: "http://m.studypointshare.com/user-articles.html?courseid="+ res.id, // 分享链接
                    imgUrl: firstImg, // 分享图标
                });

                //  点击图片预览
                $(".aec-picture").click(function () {
                    var self = this;
                    weChat.chooseImg($(self).find("img").attr("src"),picArr);
                })
            }
        });
    }

    //播放录音与暂停
    function toPlay(_self) {
        var timeout;
        var audio = $(_self).find(".j-audio")[0],
            self = _self,
            duration = audio.duration,
            trueduration = new Number($(self).find(".videonum").find("a").html()),
            currentTime = audio.currentTime;

        if (currentTime == 0) {
            audio.play();
            $(self).find(".playicon").find("img").attr("src","images/play.gif");
            timeout = setTimeout(function () {
                $(self).find(".playicon").find("img").attr("src","images/audio.png");
            },trueduration * 1000);
        } else if (currentTime == duration) {
            audio.load();
            audio.play();
            $(self).find(".playicon").find("img").attr("src","images/play.gif");
            timeout = setTimeout(function () {
                $(self).find(".playicon").find("img").attr("src","images/audio.png");
            },trueduration * 1000);
        } else {
            if (ispause) {
                trueduration = trueduration - currentTime;
                timeout = setTimeout(function () {
                    $(self).find(".playicon").find("img").attr("src","images/audio.png");
                },trueduration * 1000);
                audio.play();
                $(self).find(".playicon").find("img").attr("src","images/play.gif");
            } else {
                clearTimeout(timeout);
                audio.pause();
                $(self).find(".playicon").find("img").attr("src","images/audio.png");
            }
            ispause = !ispause;
        }

    }
});


